/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mbean.admin.customerdemand;

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import model.entity.Customer;
import model.entity.Customerdemand;
import model.entity.Role;
import model.entity.User;
import org.omnifaces.util.Faces;
import org.omnifaces.util.Messages;
import service.ICustomerdemandManager;
import service.IUserService;
import service.InterfaceServiceLocal;

/**
 *
 * @author dingnangen
 */
@Named(value = "customerdemandCreateMB")
@SessionScoped
public class CustomerdemandCreateMB implements Serializable {

    /**
     * Creates a new instance of CustomerdemandCreateMB
     */
    public CustomerdemandCreateMB() {
    }
    @EJB(beanName = "CustomerdemandManager")
    private ICustomerdemandManager<Customerdemand> customerdemandManager;
    @EJB(beanName = "CustomerManager")
    private InterfaceServiceLocal<Customer> customerManager;
    @EJB(beanName = "RoleManager")
    private InterfaceServiceLocal<Role> roleManager;
    @EJB(beanName = "UserManager")
    private IUserService<User> userManager;
    private Customerdemand currentcustomerdemand;
    private Customer currentCustomer;
    private String currentCustomerid;
    private List<Customer> Customerlist;
    private User currentUser;
    private boolean newcustomer;

    public void init() {

        newcustomer = false;
        currentcustomerdemand = new Customerdemand();
        if (Customerlist == null) {
            Customerlist = customerManager.findAll();
        }
        currentCustomer = new Customer();
        currentcustomerdemand.setIdcustomer(currentCustomer);
        currentcustomerdemand.setServicedate(new Date());
        currentcustomerdemand.setStatus(0);
        currentCustomerid = "";
        currentUser = null;
    }

    public void precreateCustomerdemand() {

        if (Faces.isPostback()) {
            System.out.println("isPostback");
            return;
        }
        init();
    }

    public void create() {

        System.out.println("currentCustomerid:" + currentCustomerid);

        if (this.newcustomer) {
            currentUser = new User();
            currentUser.setName(currentCustomerid);
            currentUser.setTel(currentcustomerdemand.getTel());
            currentUser.setLoginname(currentcustomerdemand.getTel());
            currentUser.setPassword("21218CCA77804D2BA1922C33E0151105");
            currentUser.setLastLogin(new Date());
            List<Role> roleList = new ArrayList();
            Role role = roleManager.find(1);
            System.out.println(role.getRoleName());
            roleList.add(role);
            currentUser.setRoleList(roleList);
            this.currentCustomer.setIduser(currentUser);

            this.currentCustomer.setLatitude(currentcustomerdemand.getLatitude());
            this.currentCustomer.setLongitude(currentcustomerdemand.getLongitude());
            this.currentCustomer.setAddress(currentcustomerdemand.getAddress());
            userManager.create(currentUser);
            customerManager.create(currentCustomer);

        } else {
            currentCustomerid = currentCustomerid.split(":")[0];
            currentCustomer = customerManager.find(Integer.parseInt(currentCustomerid));
            currentcustomerdemand.setIdcustomer(currentCustomer);

        }

        customerdemandManager.create(currentcustomerdemand);
        System.out.println("create finish");
        Messages.create(Faces.getResourceBundle("bundle").getString("saveSuccess")).add();
        init();
//        List<Vehicle> vehicleList2 = new ArrayList<Vehicle>();
//        vehicleList.forEach(s -> {
//            Vehicle edit = vehicleManager.edit(s);
//            edit.setIddepot(currentdepot);
//            vehicleList2.add(edit);
//        });
//        currentdepot.setVehicleList(vehicleList2);
//        depotManager.edit(this.currentdepot);
    }

    public void oncustomerchange() {
        currentCustomerid = currentCustomerid.split(":")[0];
        try {
            currentCustomer = customerManager.find(Integer.parseInt(currentCustomerid));
            if (currentCustomer != null) {
                System.out.println("currentcustomerdemand.setAddress(currentCustomer.getAddress()):" + currentCustomer.getAddress());
                currentcustomerdemand.setAddress(currentCustomer.getAddress());
                currentcustomerdemand.setLatitude(currentCustomer.getLatitude());
                currentcustomerdemand.setLongitude(currentCustomer.getLongitude());
                currentcustomerdemand.setTel(currentCustomer.getIduser().getTel());

            }
        } catch (Exception e) {
            System.out.println("currentCustomer is null");
        }
    }

    public Customerdemand getCurrentcustomerdemand() {
        return currentcustomerdemand;
    }

    public void setCurrentcustomerdemand(Customerdemand currentcustomerdemand) {
        this.currentcustomerdemand = currentcustomerdemand;
    }

    public Customer getCurrentCustomer() {
        return currentCustomer;
    }

    public void setCurrentCustomer(Customer currentCustomer) {
        this.currentCustomer = currentCustomer;
    }

    public String getCurrentCustomerid() {
        return currentCustomerid;
    }

    public void setCurrentCustomerid(String currentCustomerid) {
        this.currentCustomerid = currentCustomerid;
    }

    public List<Customer> getCustomerlist() {
        return Customerlist;
    }

    public void setCustomerlist(List<Customer> Customerlist) {
        this.Customerlist = Customerlist;
    }

    public User getCurrentUser() {
        return currentUser;
    }

    public void setCurrentUser(User currentUser) {
        this.currentUser = currentUser;
    }

    public boolean isNewcustomer() {
        return newcustomer;
    }

    public void setNewcustomer(boolean newcustomer) {
        this.newcustomer = newcustomer;
    }

}
